<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'客户列表'),
);
$this->model('model')->checkjuese(7);
$juese_id=7;
$workerids=$this->model('model')->getworkerids($juese_id);
$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['workers']=$this->getall('worker',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
//客户列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('worker'.$juese_id.'id'=>$workerids,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['jibie_type'] && $_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type']?:'all';
	if($_RQ['status'] && $_RQ['status']!='all'){
		if($_RQ['status']==7){
			$params['ti7_time >']=100;
		}elseif($_RQ['status']=='t5liu_time'){
			$params['t5liu_time >']=100;
		}elseif($_RQ['status']=='t5wu_time'){
			$params['t5wu_time >']=100;
		}elseif($_RQ['status']=='t6liu_time'){
			$params['t6liu_time >']=100;
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['up_time']){
		$time_range=cfc('tools')->base64_decode($_RQ['up_time'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['up_time between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['up_time']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['id']);
			if($item['from_type']==1){
				$item['from_typetitle']=$this->member_from1_type[$item['from1_type']];
			}elseif($item['from_type']==2){
				$item['from_typetitle']=$this->member_from2_type[$item['from2_type']];
			}else{
				$item['from_typetitle']=$this->member_from_type[$item['from_type']];
			}
			$item['up_time']=$item['up_time']?date('Y/m/d',$item['up_time']):'无';
			$genjin_log=pdo_get('jy_yxw_genjin_log',array('memberid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'remark','createtime desc');
			$item['genjinmsg']=$genjin_log['remark']?:'无任何根据记录';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		//'export'=>'export',
		'list'=>array('data'=>'list','line_max_width'=>'900px','del_message'=>'确认删除该客户吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			//array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
			/*array('name'=>'jibie_type','title'=>'客户级别','type'=>'radio','options'=>'jibie_type'),
			array('name'=>'from_typetitle','title'=>'渠道','type'=>'string'),
			array('title'=>'网络员','name'=>'worker3id','type'=>'select','options'=>'workers'),
			array('title'=>'前台专员','name'=>'worker4id','type'=>'select','options'=>'workers'),
			array('title'=>'电客员','name'=>'worker5id','type'=>'select','options'=>'workers'),
			array('title'=>'销售员','name'=>'worker6id','type'=>'select','options'=>'workers'),
			array('title'=>'体验员','name'=>'worker7id','type'=>'select','options'=>'workers'),
			array('title'=>'试用员','name'=>'worker8id','type'=>'select','options'=>'workers'),*/
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'up_time','title'=>'最近跟进时间','type'=>'string'),
			array('name'=>'genjinmsg','title'=>'最近跟进记录','type'=>'string','ishuan'=>1),
			//array('name'=>'createtime','title'=>'添加日期','type'=>'time'),
		),'action'=>array(
			//array('type'=>'href','op'=>'setgenjin_log','title'=>'处理电客情况','show'=>array('status'=>array(1,2,30,31))),
			//array('type'=>'href','op'=>'setgenjin_log6','title'=>'处理跟进情况','show'=>array('status'=>array(32,6,8))),
			array('type'=>'href','action'=>'jy_yxw/xiaoshou.setgenjin_log/set7/','title'=>'处理体验体用','show'=>array('status'=>array(5,6,40,42))),
			//array('type'=>'href','action'=>'jy_yxw/juese7.list/setgenjin_log8','title'=>'处理试用情况','show'=>array('status'=>array(7))),
			//array('type'=>'href','op'=>'setgenjin_log9','title'=>'处理成交情况','show'=>array('status'=>array(9))),
			//array('type'=>'modal','op'=>'setstatus4','title'=>'处理已意向','show'=>array('status'=>array(3))),
			//array('type'=>'modal','op'=>'worker6id','title'=>'分配销售员','show'=>array('status'=>4)),
			//array('type'=>'modal','op'=>'worker7id','title'=>'分配体验师','show'=>array('status'=>33)),
			//array('type'=>'modal','op'=>'worker8id','title'=>'分配试用员','show'=>array('status'=>34)),
			//array('type'=>'modal','op'=>'worker9id','title'=>'分配售后助理员','show'=>array('status'=>array(10,35))),
			array('type'=>'href','action'=>'/member.genjin_log/onelist/','title'=>'查看跟进记录'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'客户名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status2'),
			array('name'=>'up_time','width'=>15,'title'=>'时间段','type'=>'time_range')
		),
	);
	$return['status']=$this->model('member')->gestatus('all');
	$return['status2']=$this->model('member')->gestatus(7);
	$return['status2']['all']='全部';
	$return['jibie_type']=$this->member_jibie_type;
	$return['from_type']=$this->member_from_type;
	$return['from1_type']=$this->member_from1_type;
	$return['from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('member')->add($postdata);
		exi('添加成功','','/store.s_item.counts.list//status=0');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>$tabs,
		'edit'=>$this->model('member')->edit_item(),
	);
	$return['sex']=$this->sex;
	$return['xueli_type']=$this->xueli_type;
	$return['from_type']=$this->member_from_type;
	$return['member_from1_type']=$this->member_from1_type;
	$return['member_from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$return['params']=array();
	$params=array('worker'.$juese_id.'id'=>$workerids,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['jibie_type'] && $_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type']?:'all';
	if($_RQ['status'] && $_RQ['status']!='all'){
		if($_RQ['status']==7){
			$params['ti7_time >']=100;
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$list=pdo_getall('jy_yxw_member',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['jibie_type']=$this->member_jibie_type[$item['jibie_type']];
			$item['from_type']=$this->member_from_type[$item['from_type']];
			$item['status']=$this->member_status[$item['status']];
			$item['xueli']=$this->xueli_type[$item['xueli']];
		}
	}
	$header=array(
		'id'=>'客户ID',
		'title'=>'客户名称',
		'mobile'=>'手机号',
		'jibie_type'=>'级别',
		'from_type'=>'渠道',
		'xueli'=>'学历',
		'status'=>'状态',
		'createtime'=>'注册时间',
	);
	model('core/tools')->export2excel($header,$list,'客户数据');
}
if($_RQ['op']=='setgenjin_log' ||  $_RQ['op']=='setgenjin_log6' ||  $_RQ['op']=='setgenjin_log7' ||  $_RQ['op']=='setgenjin_log8' ||  $_RQ['op']=='setgenjin_log9'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->model('uplog')->addgenjin_log($post);
		$update=array('up_time'=>time());
		if($post['status']==2){
			$status=6;
		}elseif($post['status']==0 || $post['status']==3){
			$status=32;
			$update['ti6_time']=0;
			$update['ti5_time']=0;
		}elseif($post['status']==1){
			$status=40;
		}elseif($post['status']==4){
			$status=42;
			$update['ti6_time']=time();
		}elseif($post['status']==5){
			$status=34;
		}
		$update['status']=$status;
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['memberid']));
		exi('操作成功！','','//list/status='.$status);
	}
	if($_RQ['id']){
		$item['memberid']=$_RQ['id'];
		$member=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['workerid']=$member['worker5id'];
	}else{
		exi('请先选择客户','error');
	}
	if($member['status']<=40){
		$item['status']=1;
	}else{
		$item['status']=4;
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'处理体验体用',
	);
	$htmls['edit']=array(
		array('type'=>'text','title'=>'跟进信息','name'=>'remark'),
		array('type'=>'radio','title'=>'跟进状态','name'=>'status','options'=>'status'),
		//array('type'=>'radio','title'=>'跟进效果','name'=>'type','options'=>'genjin_type','show'=>array('status'=>array(2))),
		//array('type'=>'time','title'=>'回访时间','name'=>'huifang_time','show'=>array('status'=>array(0,2))),
	);
	$return['status']=$this->genjin_status;
	if($_RQ['op']=='setgenjin_log6'){
		$return['genjin_type']=$this->genjin_type2;
	}elseif($_RQ['op']=='setgenjin_log7'){
		$return['genjin_type']=$this->genjin_type3;
		$return['status']=array('体验失败','体验中','体验结束','体用失败','体用中','体用结束');
		unset($htmls['edit'][3]);
	}elseif($_RQ['op']=='setgenjin_log8'){
		$return['genjin_type']=$this->genjin_type4;
		$return['status']=array('体用失败','体用中','体用完成');
		unset($htmls['edit'][3]);
	}elseif($_RQ['op']=='setgenjin_log9'){
		$return['genjin_type']=$this->genjin_type5;
		unset($htmls['edit'][3]);
	}else{
		$return['genjin_type']=$this->genjin_type1;
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setstatus4'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$update=array('up_time'=>time());
		if(!$post['status']){
			$update['status']=4;
			$post['type']=4;
			$post['status']=2;
		}
		if($post['status']==1){
			$update['status']=2;
			$post['type']=3;
			$post['status']=2;
		}
		$post['remark']='客户已意向';
		$this->model('uplog')->addgenjin_log($post);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','//list/status=4');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['memberid']=$item['id'];
		unset($item['id']);
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'处理意向',
	);
	$htmls['edit']=array(
		array('type'=>'radio','title'=>'意向状态','name'=>'status','options'=>'status'),
		array('type'=>'text','title'=>'未按时意向原因','name'=>'remark','show'=>array('status'=>array(1))),
	);
	$return['status']=array('已意向','未按时意向');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='worker6id'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$update=array('up_time'=>time(),'worker6id'=>intval($post['worker6id']));
		if($post['worker6id']){
			$update['status']=32;
		}
		if(!$post['worker6id']){
			$update['status']=4;
		}
		$uplog=array(
			'memberid'=>$id,
			'remark'=>'分配销售员',
			'status'=>1,
			'type'=>2,
		);
		$this->model('uplog')->addgenjin_log($uplog);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','//list/status=4');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'分配销售员',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'分配销售员','name'=>'worker6id','options'=>'workers'),
	);
	$return['workers']=$this->getall('worker',array('status'=>1,'juese'=>array(9,10,11,12,13),'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='worker8id'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$update=array('up_time'=>time(),'worker8id'=>intval($post['worker8id']));
		if($post['worker8id']){
			$update['status']=7;
		}
		if(!$post['worker8id']){
			$update['status']=34;
		}
		$uplog=array(
			'memberid'=>$id,
			'remark'=>'分配试用员',
			'status'=>1,
			'type'=>2,
		);
		$this->model('uplog')->addgenjin_log($uplog);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','//list/status=34');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'分配试用员',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'分配试用员','name'=>'worker8id','options'=>'workers'),
	);
	$return['workers']=$this->getall('worker',array('status'=>1,'juese'=>8,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='worker9id'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$update=array('up_time'=>time(),'worker9id'=>intval($post['worker9id']));
		if($post['worker9id']){
			$update['status']=36;
		}
		if(!$post['worker9id']){
			$update['status']=35;
		}
		$uplog=array(
			'memberid'=>$id,
			'remark'=>'分配售后助理员',
			'status'=>1,
			'type'=>2,
		);
		$this->model('uplog')->addgenjin_log($uplog);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','//list/status=35');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'分配售后助理员',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'分配售后助理员','name'=>'worker9id','options'=>'workers'),
	);
	$return['workers']=$this->getall('worker',array('status'=>1,'juese'=>9,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->model('member')->check_del($id,'xiaoshou');
			pdo_delete('jy_yxw_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}